package cn;

import cn.autumnorange.authorization.security.properties.OAuth2Properties;
import cn.autumnorange.authorization.security.utils.JsonUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.codec.Base64;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.Principal;

/**
 * Created on 2018/4/28.
 *
 * @author zlf
 * @since 1.0
 */
// @ComponentScan(basePackages = "cn.autumnorange.user.commons.entity.*")
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
@RestController
@Slf4j
public class AuthorizationServerApplication {

  @Autowired private OAuth2Properties oAuth2Properties;

  public static void main(String[] args) {
    String encode = new String(Base64.encode("merryyou:merryyou".getBytes()));
    log.info("encode:merryyou \n {}", encode);

    encode = new String(Base64.encode("zuul:zuul".getBytes()));
    log.info("encode:zuul\n {}", encode);

    encode = new String(Base64.encode("client-a:client-a".getBytes()));
    log.info("encode:client-a\n {}", encode);

    encode = new String(Base64.encode("order:order".getBytes()));
    log.info("encode:order \n {}", encode);
    SpringApplication.run(AuthorizationServerApplication.class, args);
  }

  @GetMapping("/userJwt")
  public Object getCurrentUserJwt(Authentication authentication, HttpServletRequest request)
      throws UnsupportedEncodingException {
    log.info(
        "【SecurityOauth2Application】 getCurrentUserJwt authentication={}",
        JsonUtil.toJson(authentication));

    String header = request.getHeader("Authorization");
    String token = StringUtils.substringAfter(header, "bearer ");

    Claims claims =
        Jwts.parser()
            .setSigningKey(oAuth2Properties.getJwtSigningKey().getBytes("UTF-8"))
            .parseClaimsJws(token)
            .getBody();
    String blog = (String) claims.get("blog");
    log.info("【SecurityOauth2Application】 getCurrentUser1 blog={}", blog);

    return authentication;
  }

  @GetMapping("/userRedis")
  public Object getCurrentUserRedis(Authentication authentication) {
    log.info(
        "【SecurityOauth2Application】 getCurrentUserRedis authentication={}",
        JsonUtil.toJson(authentication));

    return authentication;
  }

  @GetMapping("/user/me")
  public Principal user(Principal user) {
    return user;
  }

  @GetMapping("/test")
  public String user(String test) {
    return test;
  }

//  @Bean
//  public ServletRegistrationBean MyServlet1() {
//    return new ServletRegistrationBean(new InitAccessTokenServlet(), "/InitAccessTokenServlet");
//  }


}
